const app = getApp()
import { formList } from '../../utils/const'
import WxValidate from '../../utils/WxValidate'
import { rules, messages } from '../../utils/joinValidata'
import { query, add, update } from '../../utils/db';
import { computed } from '../../utils/vue-extend';

Page({
  data: {
    formList,
    isSumbit: false,
    originData: {},
    customItem: '',
    entered: false,
    avatarID: null,
    name: '',
    gender: 0,
    phone: null,
    region: ['北京市', '', ''],
    address: '',
    industry: '',
    size: 0,
    klass: [0, 0, 0],
    hotel: 0,
    time: '00:00',
    day: 0,
    note: '',
  },

  onLoad: function (options) {
    computed(this, {
      canSubmit() {
        const { isSumbit, name } = this.data
        // const hasClass = klass[0] && klass[1] && klass[2]
        // const hasInfo = hasClass && avatarID && name && size && phone
        return  !isSumbit && name
      }
    })
    this.initValidate()
    const entered = app.globalData.entered
    this.setData({ entered: entered })
    if(!entered) { return }
    query('entryForms', { _openid: app.globalData._openid }).then(res => {
      const data = res[0]
      const fields = this.data.formList.fields
      const newData = {}
      const originData = {}
      fields.forEach(field => {
        newData[field] = data[field]
        originData[field] = data[field]
      })
      this.setData({ originData })
      fields.forEach(field => {
        this.setData({ [field]: newData[field] })
      })
    })
  },

  initValidate() {
    this.WxValidate = new WxValidate(rules, messages)
    this.WxValidate.addMethod('klass', value => value[0] && value[1] && value[2])
    this.WxValidate.addMethod('avatarID', value => value)
    this.WxValidate.addMethod('region', value => value.length && value[0] && value[1] && value[2])
    this.WxValidate.addMethod('size', value => !!value)
  },

  formSubmit(e) {
    const form = this.data
    const postData = Object.assign({
      avatarID: form.avatarID,
      gender: form.gender,
      region: form.region,
      klass: form.klass,
      size: form.size,
      hotel: form.hotel,
      day: form.day,
      time: form.time,
      userInfo: app.globalData.userInfo,
      updated: Date.now(),
    }, e.detail.value) 
    if (!this.WxValidate.checkForm(postData)) {
      const error = this.WxValidate.errorList[0]
      wx.showToast({ icon: 'none', title: error.msg, })
      return false
    }

    this.setData({ isSumbit: true })
    if (this.data.entered) {
      this.updatePerson(postData)
    } else {
      postData.created = Date.now()
      this.setData({canSubmit: false})
      this.addPerson(postData)
    }
  },
  formReset(e) {
    if (this.data.entered) {
      this.setData({ form: this.data.originForm })
      console.log(this.data.originForm)
    } else {
      this.setData({
        avatarID: null,
        name: '',
        gender: 0,
        phone: null,
        region: ['北京市', '', ''],
        address: '',
        industry: '',
        size: 0,
        klass: [0, 0, 0],
        hotel: 0,
        time: '00:00',
        day: 0,
        note: '',
      })
    }
  },

  onNameInput: function(e) {
    this.setData({ name: e.detail.value })
  },
  onPhoneInput: function(e) {
    this.setData({ phone: e.detail.value })
  },
  bindGenderChange: function(e) {
    this.setData({ gender: e.detail.value })
  },
  bindRegionChange: function(e) {
    this.setData({ region: e.detail.value, })
  },
  bindSizeChange: function(e) {
    this.setData({ size: e.detail.value })
  },

  bindClassChange(e) {
    this.setData({ klass: e.detail.value })
  },

  bindDayChange(e) {
    this.setData({ day: e.detail.value })
  },

  bindTimeChange(e) {
    this.setData({ time: e.detail.value })
  },

  bindHotelChange(e) {
    this.setData({ hotel: e.detail.value })
  },

  onUpload(e) {
    this.setData({ avatarID: e.detail })
  },
  addPerson(postData) {
    add('entryForms', postData).then(res => {
      wx.showModal({
        icon: 'success',
        content: '报名成功,非常期待我们的相聚',
        confirmText: '好的',
        showCancel: false,
        success() {
          wx.reLaunch({ url: '/pages/index/index', })
        }
      })
    })
  },
  updatePerson(updateData) {
    update('entryForms', app.globalData.formid, updateData).then(res => {
      wx.showModal({
        icon: 'success',
        content: '报名表修改成功',
        confirmText: '我知道了',
        showCancel: false,
        success() {
          wx.reLaunch({ url: '/pages/index/index', })
        }
      })
    })
  }
})